package com.lq.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.lq.Dao.UserDao;
import com.lq.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lq.pojo.CodeMessage;
import com.lq.pojo.Grade;
import com.lq.pojo.Shopping;
import com.lq.pojo.Student;
import com.lq.pojo.User;
import com.lq.service.ShoppingService;
import com.lq.service.UserService;
import com.lq.util.ByteConversionUtil;
import com.lq.util.FiltPrint;
import com.lq.util.Pager;

@Controller
public class UserController {
	@Autowired
	UserService userService;
	
	@Autowired
	ShoppingService shoppingService;
	
	private static Logger logger=LoggerFactory.getLogger(UserController.class);

		/**
		 * 跳转到主页
		 * @return
		 */
		@RequestMapping(value = "/hello")
		public String pageString() {
			logger.info("这是info日志");
			logger.warn("这是错误信息");
			return "redirect:add.html";
		}
		

		
		

		
		//查询所有商品
		@RequestMapping(value = "aa")
		@ResponseBody
		public Map<String,Object> pagestring4(String name,String assat){
			Map<String,Object>maps=new HashMap<>();
			List<Shopping> selByShoppingAll = shoppingService.selByShoppingAll();
			ListIterator<Shopping> shoppingListIterator = selByShoppingAll.listIterator();
			while (shoppingListIterator.hasNext()){
				maps.put("user",shoppingListIterator.next());
			}
			maps.put("name",name);
			maps.put("assat",assat);
			System.out.println(selByShoppingAll.size());
			return maps;
		}



		
		@RequestMapping("demo5")
		@ResponseBody
		public String pageString5(String name,int age) {
			User users=new User();
			users.setName(name);
			users.setPassword("13254");
			User users2=new User();
			users2.setName("王五");
			users2.setPassword("12456");
			User users3=new User();
			users3.setName("小王");
			users3.setPassword("12456");
			List<User>list=new ArrayList<User>();
			list.add(users);
			list.add(users2);
			list.add(users3);
			Map<String,Object>map=new HashMap<String, Object>();
			map.put("部门名称","研发部");
			map.put("部门成员",list);
			map.put("部门位置","武汉光谷");
			//String jsonString2 = JSONArray.toJSONString(map);
			String jsonString3=JSONObject.toJSONString(list);
			String jsonString4=JSONArray.toJSONString(list);
			System.out.println("jsonObject:"+jsonString3);
			System.out.println("JSONArray:"+jsonString4);
			return jsonString3;
	}
		
		
	//注册：账号，密码，个人头像设置
	@RequestMapping("/testuser")
	@ResponseBody
	public String pagestring9(@RequestParam("file") MultipartFile file,User user) {
		if(file.isEmpty()) {
			return "上传失败，请选择文件";
		}
		try {
			File file2 = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX+"static/images/");
			String filepathzz=file2.getPath();
			String originalFilename = file.getOriginalFilename();
	        String extension = originalFilename.substring(originalFilename.lastIndexOf('.'));
	        //生成随机名
	        String fileName = UUID.randomUUID() + extension;
			String zuizuiString=filepathzz+"/"+fileName;
			System.out.println("地址的路径:"+zuizuiString);
			file.transferTo(new File(zuizuiString));
			System.out.println("文件上传成功");
			String md5Password = MD5Util.getMd5Password(user.getPassword());
			int addByUser = userService.addByUser(user.getName(),md5Password,fileName);
			if(addByUser>0){
				logger.info("个人信息保存成功");
				return "rediret:login.html";
			}else{
				logger.info("保存失败,开始回滚");
				return  "保存失败开始回滚";
			}
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			logger.warn("文件上传失败");
			e.printStackTrace();
		}
		return CodeMessage.SUCCESS;	
	}	


	//用户登录(判断账号密码是否正确)
	@RequestMapping(value = "showindexinfo")
	public String pageString20(HttpServletRequest request,Model model) throws Exception {
		logger.info("用户开始登录");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String md5Password = MD5Util.getMd5Password(password);
		logger.info("密码开始加密中"+md5Password);
		List<User> userList = userService.selByname(name,md5Password);
		if(userList!=null&&userList.size()>0){
			model.addAttribute("id",userList.get(0).getId());
			model.addAttribute("name",userList.get(0).getName());
			model.addAttribute("password",userList.get(0).getPassword());
			model.addAttribute("personalicon",userList.get(0).getPersonalIcon());
			logger.info("用户"+name+"登录成功");
			return "showinfo.html";
		}
			logger.info("用户"+name+"登录失败");
			model.addAttribute("error","密码错误");
			return "login.html";
	}
	
	
	
	
	
	@RequestMapping("demo6")
	@ResponseBody
	public String 	pageString6() {
		/*int totalRecord = usersService.getCount();		 
		Pager pager=new Pager(1,5, totalRecord); //1.当前页数   2. 每页显示数据条数   3.总条数
		int startIndex=pager.getStartIndex();
		usersService.getPageUsers(startIndex,pager.getPageSize());*/
		Grade grade=new Grade();
		grade.setId(1);
		grade.setName("admin");
		Student student=new Student();
		student.setId(12);
		student.setName("小红");	
		Student student2=new Student();
		student2.setId(14);
		student2.setName("小明");
		grade.addStudent(student);
		grade.addStudent(student2);
		//转换为json
		String jsonString=JSONArray.toJSONString(grade);
		System.out.println("JSON字符串:"+jsonString);
		
		 String jsonString2 = JSONObject.toJSONString(grade);
		 System.out.println("JSON字符串2"+jsonString2);
		 
		 //第三种方式  父类
		 String jsonString3 = JSON.toJSONString(grade);
		 System.out.println("JSON父类"+jsonString3);
		 
		//反序列化 转换为对象-
		Grade grade3=JSONArray.parseObject(jsonString,Grade.class);
		System.out.println("JAVABean对象"+grade3);
		return "开始";
	}
	
	
		
}	
